package com.deltapunkt.proxy.factory;

import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

import com.deltapunkt.proxy.main.ForwardPair;

public class Settings {
	private final ForwardPair fp;
	private final ProxyType type;

	public Settings(ForwardPair fp, ProxyType type) {
		this.fp = fp;
		this.type = type;
	}

	public ForwardPair getForward() {
		return fp;
	}

	public ProxyType getType() {
		return type;
	}

	public static Settings getFromCfgFile(String path) {
		Properties p = new Properties();
		try {
			p.load(new FileReader(path));
			p.list(System.out);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}

		String srcAddr = p.getProperty("SrcAddr", "");
		int srcPort = Integer.parseInt(p.getProperty("SrcPort", "8080"));
		String dstAddr = p.getProperty("DstAddr", "");
		int dstPort = Integer.parseInt(p.getProperty("DstPort", "1433"));

		ForwardPair fp = new ForwardPair(srcAddr, srcPort, dstAddr, dstPort);
		ProxyType type = ProxyType.valueOf(p.getProperty("Type"));

		return new Settings(fp, type);
	}
}
